<template>
  <div class="questionnaire-submit">
    <el-card v-loading="loading">
      <template #header>
        <div class="card-header">
          <h2>{{ questionnaire.title }}</h2>
          <p class="description">{{ questionnaire.description }}</p>
        </div>
      </template>

      <el-form ref="form" :model="answers" label-width="0">
        <div v-for="(question, index) in questionnaire.questions" :key="index" class="question-item">
          <div class="question-title">
            <span class="question-index">{{ index + 1 }}.</span>
            {{ question.title }}
            <span class="required" v-if="question.required">*</span>
          </div>

          <!-- 单选题 -->
          <el-form-item 
            v-if="question.type === 1"
            :prop="String(question.id)"
            :rules="question.required ? [{ required: true, message: '请选择答案', trigger: 'change' }] : []"
          >
            <el-radio-group v-model="answers[question.id]">
              <el-radio 
                v-for="option in question.options" 
                :key="option.id" 
                :label="option.content"
              >
                {{ option.content }}
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- 多选题 -->
          <el-form-item 
            v-else-if="question.type === 2"
            :prop="String(question.id)"
            :rules="question.required ? [{ required: true, message: '请选择答案', trigger: 'change' }] : []"
          >
            <el-checkbox-group v-model="answers[question.id]">
              <el-checkbox 
                v-for="option in question.options" 
                :key="option.id" 
                :label="option.content"
              >
                {{ option.content }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>

          <!-- 填空题 -->
          <el-form-item 
            v-else-if="question.type === 3"
            :prop="String(question.id)"
            :rules="question.required ? [{ required: true, message: '请填写答案', trigger: 'blur' }] : []"
          >
            <el-input 
              v-model="answers[question.id]"
              type="textarea"
              :rows="3"
              placeholder="请输入您的答案"
            />
          </el-form-item>
        </div>
      </el-form>

      <div class="form-actions">
        <el-button type="primary" :loading="submitting" @click="handleSubmit">提交问卷</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getDetail, submit } from '@/api/questionnaire'

const route = useRoute()
const router = useRouter()
const form = ref(null)
const loading = ref(false)
const submitting = ref(false)
const questionnaire = ref({
  title: '',
  description: '',
  questions: []
})
const answers = ref({})

// 加载问卷详情
const loadDetail = async () => {
  loading.value = true
  try {
    const res = await getDetail(route.params.id)
    if (res.code === 200) {
      questionnaire.value = res.data
    } else {
      ElMessage.error(res.message || '加载问卷失败')
    }
  } catch (error) {
    console.error('加载问卷失败:', error)
    ElMessage.error('加载问卷失败')
  } finally {
    loading.value = false
  }
}

// 提交问卷
const handleSubmit = async () => {
  if (!form.value) return
  
  try {
    await form.value.validate()
  } catch (error) {
    return ElMessage.warning('请完成所有必填题目')
  }

  submitting.value = true
  try {
    const res = await submit(route.params.id, answers.value)
    if (res.code === 200) {
      ElMessage.success('提交成功')
      router.push('/questionnaire/my-submissions')
    } else {
      ElMessage.error(res.message || '提交失败')
    }
  } catch (error) {
    console.error('提交问卷失败:', error)
    ElMessage.error('提交失败')
  } finally {
    submitting.value = false
  }
}

onMounted(() => {
  loadDetail()
})
</script>

<style scoped>
.questionnaire-submit {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  text-align: center;
}

.description {
  color: #666;
  margin: 10px 0;
}

.question-item {
  margin-bottom: 30px;
}

.question-title {
  font-size: 16px;
  margin-bottom: 15px;
  font-weight: 500;
}

.question-index {
  margin-right: 8px;
  font-weight: bold;
}

.required {
  color: #f56c6c;
  margin-left: 4px;
}

.form-actions {
  margin-top: 30px;
  text-align: center;
}

:deep(.el-radio), :deep(.el-checkbox) {
  margin-right: 30px;
  margin-bottom: 10px;
}

:deep(.el-radio__label), :deep(.el-checkbox__label) {
  white-space: normal;
}

:deep(.el-form-item__error) {
  padding-top: 4px;
}
</style> 